class Solution(object):
    def sumEvenGrandparent(self, root):
        self.ans = 0

        def search(node, prev1, prev2):
            if prev1 % 2 == 0:
                self.ans += node.val
            if node.left:
                search(node.left, prev2, node.val)
            if node.right:
                search(node.right, prev2, node.val)

        search(root, -1, -1)
        return self.ans
